package adventure.commands;

import adventure.*;
import colour.ColourUtil;
import player.*;

/**
 *
 * @author Michael Hanns
 *
 */
public class Group extends AbstractCommand {

    public Group(Adventure a) {
        super(a, new String[]{"GROUP", "PARTY"});
    }

    @Override
    public String validateAndExecute(Player player, String[] args) {
        if (args.length == 1) {
            return viewGroup(player);
        } else if (args.length == 2 && args[1].equalsIgnoreCase("leave")) {
            return leaveGroup(player);
        } else if (args.length == 2 && args[1].equalsIgnoreCase("remove")) {
            return "Remove who group your group?";
        } else if (args.length > 2 && args[1].equalsIgnoreCase("remove")) {
            String target = args[2];
            for (int x = 3; x < args.length; x++) {
                target += " " + args[x];
            }
            return removeFromGroup(player, target);
        } else {
            if (((AdventureEdit) adv).getRoomManager().isLightPresent(
                    player.x(), player.y(), player.z())) {
                return addToGroup(player, Util.consolidateArguments(args));
            } else {
                return "You cannot see anything in the darkness!";
            }
        }
    }

    private String addToGroup(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        int targetPlayer = rooms.getRoomPlayer(x, y, z, target);
        if (player.getID() == targetPlayer) {
            return "You cannot group yourself!";
        } else if (targetPlayer > 0) {
            target = players.getOnlinePlayer(targetPlayer).getName();
            if (player.addToGroup(players.getOnlinePlayer(targetPlayer))) {
                out.broadcastToPlayer(players.getOnlinePlayer(targetPlayer),
                        ColourUtil.colourise(player.getAliveName(), ColourUtil.magenta)
                        + " has added you to their group.");

                return "\nYou have added " + ColourUtil.colourise(target, ColourUtil.magenta)
                        + " to your group.";
            } else {
                return "\n" + ColourUtil.colourise(target, ColourUtil.magenta) + " is already in your group!";
            }
        } else {
            return "You don't see any " + target + "!";
        }

    }

    private String removeFromGroup(Player player, String target) {
        int x = player.x();
        int y = player.y();
        int z = player.z();

        int targetPlayer = rooms.getRoomPlayer(x, y, z, target);
        if (player.getID() == targetPlayer) {
            return "You cannot remove yourself from your group!";
        } else if (targetPlayer > 0) {
            target = players.getOnlinePlayer(targetPlayer).getName();
            if (player.removeFromGroup(players.getOnlinePlayer(targetPlayer))) {
                out.broadcastToPlayer(players.getOnlinePlayer(targetPlayer),
                        ColourUtil.colourise(player.getAliveName(), ColourUtil.magenta)
                        + " has removed you from their group.");

                return "\nYou have removed " + ColourUtil.colourise(target, ColourUtil.magenta)
                        + " from your group.";
            } else {
                return "\n" + ColourUtil.colourise(target, ColourUtil.magenta)
                        + " is not in your group!";
            }
        } else {
            return "You don't see any " + target + "!";
        }

    }

    private String leaveGroup(Player player) {
        Player[] group = player.playersInGroup();

        String out = "";
        if (group.length > 0) {
            out = removeFromGroup(player, group[0].getAliveName());
            for (int x = 1; x < group.length; x++) {
                out += "\n" + removeFromGroup(player, group[x].getAliveName());
            }

            return out;
        } else {
            return "You are not in a group with anyone!";
        }
    }

    private String viewGroup(Player p) {
        /* Generates a table that looks like this:
         *   +-------+
         *   | Group |
         *   +-------------------------------------------------------------+
         *   | Player                    Vocation                 Level    |
         *   +-------------------------------------------------------------+
         *   | <playername>              <playervocation>         <lvl>    |
         *   | <playername>              <playervocation>         <lvl>    |
         *   | <playername>              <playervocation>         <lvl>    |
         *   | ...                       ...                      ...      |
         *   +-------------------------------------------------------------+
         * 
         */

        // Get the players
        Player[] group = p.playersInGroup();


        // Generate the table
        String groupTab = "\n +-------+\n | "
                + ColourUtil.colourise("Group", ColourUtil.green) + " |";

        String dividerHead = "\n +-------+-----------------------------------------------------+";
        
        String divider = "\n +-------------------------------------------------------------+";

        String header = "\n | " + ColourUtil.colourise(addWhiteSpace("Name", 25)
                + " " + addWhiteSpace("Vocation", 25)
                + " " + addWhiteSpace("Level", 7), ColourUtil.green) + " |";

        String playersTable = "\n | You are not in a group with anyone.                         |";

        if (group.length > 0) {
            playersTable = "\n | " + ColourUtil.colourise(addWhiteSpace(group[0].getAliveName(), 25)
                    + " " + addWhiteSpace(group[0].getVocationName(), 25)
                    + " " + addWhiteSpace(group[0].getLevel() + "", 7), ColourUtil.magenta) + " |";

            for (int x = 1; x < group.length; x++) {
                playersTable += "\n | " + ColourUtil.colourise(addWhiteSpace(group[x].getAliveName(), 25)
                        + " " + addWhiteSpace(group[x].getVocationName(), 25)
                        + " " + addWhiteSpace(group[x].getLevel() + "", 7), ColourUtil.magenta) + " |";

            }
        }

        return groupTab + dividerHead + header + divider + playersTable + divider;
    }

    private String addWhiteSpace(String input, int maxLength) {
        if (input.length() == maxLength) {
            return input;
        } else if (input.length() < maxLength) {
            for (int x = input.length(); x < maxLength; x++) {
                input += " ";
            }

            return input;
        } else {
            return input.substring(0, maxLength - 2) + "..";
        }
    }
}
